/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Subsectionexerciseset;
import me.zhengjie.modules.system.domain.vo.SubsectionexercisesetQueryCriteria;
import me.zhengjie.modules.system.service.SubsectionexercisesetService;
import me.zhengjie.utils.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* @author shitou
* @date 2024-05-29
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "subsectionexerciseset管理")
@RequestMapping("/api/subsectionexerciseset")
public class SubsectionexercisesetController {

    private final SubsectionexercisesetService subsectionexercisesetService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('subsectionexerciseset:list')")
    public void exportSubsectionexerciseset(HttpServletResponse response, SubsectionexercisesetQueryCriteria criteria) throws IOException {
        subsectionexercisesetService.download(subsectionexercisesetService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询subsectionexerciseset")
    @ApiOperation("查询subsectionexerciseset")
//    @PreAuthorize("@el.check('subsectionexerciseset:list')")
    public ResponseEntity<PageResult<Subsectionexerciseset>> querySubsectionexerciseset(SubsectionexercisesetQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(subsectionexercisesetService.queryAll(criteria,page),HttpStatus.OK);
    }
    @PostMapping("/saveAllsubsectionexerciseset")
    @Log("新增subsectionexerciseset")
    @ApiOperation("新增subsectionexerciseset")
    public ResponseEntity<Object> saveAllsubsectionexerciseset(@Validated @RequestBody Subsectionexerciseset resources){



        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teaChchapterId",resources.getTeachChapterId());

        List<Subsectionexerciseset> subsectionexerciseset = new ArrayList<>();
        if(resources.getTeachChapterId()!=null){
            queryWrapper.eq("teachSubsectionId",resources.getTeachSubsectionId());
            subsectionexerciseset = subsectionexercisesetService.list(queryWrapper);
        } else {
            queryWrapper.eq("teachSubsectionId",null);
            subsectionexerciseset = subsectionexercisesetService.list(queryWrapper);
        }
        //get old subsectionexerciseset
        List<Integer> listIds = new ArrayList<>();
        for(Subsectionexerciseset item:subsectionexerciseset){
            listIds.add(item.getId());
        }

        //删除的
        List<Integer> delArr = new ArrayList<>();
        for(Integer id:listIds){
            if(!containsElement(resources.getExercisesetArr(),id)){
                delArr.add(id);
            }
        }



        if(delArr!=null && delArr.size()!=0){
            System.out.println("==delArr=="+delArr);
            subsectionexercisesetService.deleteAll(delArr);
        }


        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PostMapping
    @Log("新增subsectionexerciseset")
    @ApiOperation("新增subsectionexerciseset")
//    @PreAuthorize("@el.check('subsectionexerciseset:add')")
    public ResponseEntity<Object> createSubsectionexerciseset(@Validated @RequestBody Subsectionexerciseset resources){

        subsectionexercisesetService.create(resources);


        return new ResponseEntity<>(resources.getId(),HttpStatus.CREATED);
    }

    public boolean containsElement(List<Integer> array, Integer target) {
        for (int i = 0; i < array.size(); i++) {
            if (array.get(i) ==  target) {
                return true;
            }
        }
        return false;
    }
    @PutMapping
    @Log("修改subsectionexerciseset")
    @ApiOperation("修改subsectionexerciseset")
//    @PreAuthorize("@el.check('subsectionexerciseset:edit')")
    public ResponseEntity<Object> updateSubsectionexerciseset(@Validated @RequestBody Subsectionexerciseset resources){
        subsectionexercisesetService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除subsectionexerciseset")
    @ApiOperation("删除subsectionexerciseset")
    @PreAuthorize("@el.check('subsectionexerciseset:del')")
    public ResponseEntity<Object> deleteSubsectionexerciseset(@RequestBody List<Integer> ids) {
        subsectionexercisesetService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}